type Create<T> = (key: string, ...args: unknown[]) => T

/**
 * 缓存创建的实例
 * 
 * @param create 创建方法
 * @returns 
 */
export function cache<T>(create: Create<T>): Create<T> {
  const map = new Map<string,T>()

  return function(key: string, ...args: unknown[]) {
    let instance = map.get(key)
    if (!instance) {
      instance = create(key, ...args)
      map.set(key, instance)
    }
    return instance
  }
}